package com.jiuzhi.web.controller.system;

import com.jiuzhi.common.annotation.Log;
import com.jiuzhi.common.core.controller.BaseController;
import com.jiuzhi.common.core.domain.AjaxResult;
import com.jiuzhi.common.core.page.TableDataInfo;
import com.jiuzhi.common.enums.BusinessType;
import com.jiuzhi.common.utils.QRCodeUtil;
import com.jiuzhi.common.utils.poi.ExcelUtil;
import com.jiuzhi.system.domain.YxCommunity;
import com.jiuzhi.system.service.IYxCommunityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目明细Controller
 *
 * @author jiuzhi
 * @date 2020-02-15
 */
@Controller
@RequestMapping("/system/community")
public class YxCommunityController extends BaseController
{
    private String prefix = "system/community";

    @Autowired
    private IYxCommunityService yxCommunityService;

    @RequiresPermissions("system:community:view")
    @GetMapping()
    public String community()
    {
        return prefix + "/community";
    }

    /**
     * 查询项目明细列表
     */
    @RequiresPermissions("system:community:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YxCommunity yxCommunity)
    {
        startPage();
        List<YxCommunity> list = yxCommunityService.selectYxCommunityList(yxCommunity);
        return getDataTable(list);
    }

    /**
     * 导出项目明细列表
     */
    @RequiresPermissions("system:community:export")
    @Log(title = "项目明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YxCommunity yxCommunity)
    {
        List<YxCommunity> list = yxCommunityService.selectYxCommunityList(yxCommunity);
        ExcelUtil<YxCommunity> util = new ExcelUtil<YxCommunity>(YxCommunity.class);
        return util.exportExcel(list, "community");
    }

    /**
     * 新增项目明细
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    @RequestMapping(value = "/createCommonQRCode")

    public void createCommonQRCode(HttpServletResponse response, String url) throws Exception {

        ServletOutputStream stream = null;

        try {

            stream = response.getOutputStream();

//使用工具类生成二维码

            QRCodeUtil.encode(url, stream);

        } catch (Exception e) {

            e.getStackTrace();

        } finally {

            if (stream != null) {

                stream.flush();

                stream.close();

            }

        }
    }
    /**
     * 新增保存项目明细
     */
    @RequiresPermissions("system:community:add")
    @Log(title = "项目明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YxCommunity yxCommunity)
    {
        return toAjax(yxCommunityService.insertYxCommunity(yxCommunity));
    }

    /**
     * 修改项目明细
     */
    @GetMapping("/edit/{comId}")
    public String edit(@PathVariable("comId") Long comId, ModelMap mmap)
    {
        YxCommunity yxCommunity = yxCommunityService.selectYxCommunityById(comId);
        mmap.put("yxCommunity", yxCommunity);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目明细
     */
    @RequiresPermissions("system:community:edit")
    @Log(title = "项目明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YxCommunity yxCommunity)
    {
        return toAjax(yxCommunityService.updateYxCommunity(yxCommunity));
    }

    /**
     * 删除项目明细
     */
    @RequiresPermissions("system:community:remove")
    @Log(title = "项目明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(yxCommunityService.deleteYxCommunityByIds(ids));
    }
}
